<template>
  <van-popup
    v-model="dangerTaskDialog"
    position="right"
    :style="{ width:'100%',height:'100%' }"
    get-container="body"
  >
    <div v-if="dangerTaskDialog" class="history-pop-up" style="height: 100%;">
      <div class="mx-1 common-details">
        <customDetailsTopbar title="隐患排查任务详情" flag @back="$emit('update:dangerTaskDialog',false)" />
        <div class="pt-1 pb-1 px-1 d-scroll listInner bg-white">
          <div class="yhpcrwxq">
            <template>
              <custom-details-item v-for="(item,index) in descList" :key="item.field + index" :label="item.label" :value="descData[item.field]" />
            </template>
          </div>
          <template>
            <custom-radio-tag1 class="mb-1 mt-1 w-100" :list="tagList" :value="typeIndex" is-scroll @change="tagChange" />
            <el-alert v-if="radioCurrentKey === '排查隐患清单'" :title="`本次共排查出隐患${numForm['隐患总数']}个，审核通过${numForm['审核通过数量']}个，其中重大隐患${numForm['重大隐患']}个，一般隐患${numForm['一般隐患']}个。`" type="success" :closable="false" class="mb-1" />
            <div v-if="listData && listData.length>0" class="common_list table-list">
              <div v-for="(item, index) in listData" :key="index" class="list_item">
                <div class="content_area">
                  <div class="number_order">{{ index + 1 }}</div>
                  <div class="right_content">
                    <div class="content_title" style="max-width: 100%">
                      {{ radioCurrentKey === '排查隐患清单' ? item.companyName : item.score }}
                    </div>
                    <ul>
                      <li v-for="(desc,index) in listDescMap[radioCurrentKey]" :key="desc.field + index">
                        <i :class="'iconfont c-text-color' + ((index%8)+ 1)">&#xe790;</i>
                        <div class="label">{{ desc.label }}：</div>
                        <div class="value">
                          <span v-if="!desc.isFile">{{ item[desc.field] || '/' }}</span>
                          <el-link v-else type="primary" @click="openAttachPopUp(item[desc.field])">查看</el-link>
                        </div>
                      </li>
                    </ul>
                  </div>
                </div>
              </div>
            </div>
            <customLoadmore v-if="radioCurrentKey === '排查隐患清单' && listData.length>0" ref="customLoadmore" :page-num="pageNum" :pages="pages" @load="handlePages" />
            <div v-if="listData.length === 0" class="bg-white common_list">
              <el-empty />
            </div>
          </template>
        </div>
      </div>
      <attachPopUp ref="attachPopUp" />
    </div>
  </van-popup>
</template>
<script>

import CustomDetailsTopbar from '@/components/common/custom-details-topbar.vue'
import customDetailsItem from '@/components/globals/custom-details-item.vue'
import { checkCompanyContentScore, checkCompanyDetails, dangersLedgerByCompany } from '@/api/commonPage/personnel'
import commonList from '@/assets/mixin/commonList'

export default {
  name: 'DangerTasks',
  components: { customDetailsItem, CustomDetailsTopbar },
  mixins: [commonList],
  inject: ['layout'],
  props: {
    moduleName: {
      type: String,
      required: true
    },
    dangerTaskDialog: {
      type: Boolean,
      required: true
    },
    checkTaskData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      typeIndex: 0,
      listData: [],
      tagList: [
        { label: '排查隐患清单' },
        { label: '企业检查得分' }
      ],
      descList: [
        { label: '企业名称', field: 'companyName' },
        { label: '检查人员', field: 'expertName' },
        { label: '检查时间', field: 'scoreTime' },
        { label: '排查隐患数量', field: 'dangersNum' },
        { label: '检查得分', field: 'score' },
        { label: '企业评分', field: 'mark' }
      ],
      listDescMap: {
        排查隐患清单: [
          // { label: '企业名称', field: 'companyName' },
          { label: '隐患描述', field: 'describe' },
          { label: '隐患位置', field: 'position' },
          { label: '隐患等级', field: 'dangersLevel' },
          { label: '隐患类别', field: 'dangersType' },
          { label: '检查依据', field: 'checkBasis' },
          { label: '检查人员', field: 'checkPerson' },
          { label: '审核状态', field: 'auditResult' },
          { label: '检查时间', field: 'checkTime' }
        ],
        企业检查得分: [
          { label: '一级要素', field: 'oneElement' },
          { label: '二级要素', field: 'twoElement' },
          { label: '三级要素', field: 'threeElement' },
          { label: '检查事项', field: 'checkContent' },
          { label: '标准分', field: 'standardScore' },
          { label: '评分标准', field: 'gradStandards' },
          { label: '检查依据', field: 'checkGist' },
          { label: '重要程度', field: 'checkWay' },
          { label: '检查结果', field: 'checkResult' },
          { label: '评分专家', field: 'expertName' },
          { label: '涉及标准项分值', field: 'referStandardScore' },
          // { label: '得分', field: 'score' },
          { label: '资料', field: 'files', isFile: true }
        ]
      },
      descData: {
        dangersNum: 0
      },
      numForm: {},
      radioCurrentKey: '排查隐患清单',
      total: 0,
      pageNum: 1,
      pages: 0
    }
  },
  watch: {
    dangerTaskDialog(new_) {
      if (new_) {
        this.descData = this.$ldh.cloneDeep(this.checkTaskData)
        this.checkCompanyDetails()
        this.dangersLedgerByCompany()
      }
    }
  },
  methods: {
    tagChange(item) {
      this.radioCurrentKey = item.item.label
      const listFunMap = {
        排查隐患清单: this.checkCompanyDetails,
        企业检查得分: this.checkCompanyContentScore
      }
      listFunMap[this.radioCurrentKey]()
    },
    handlePages(pageNum) {
      this.pageNum = pageNum
      this.checkCompanyDetails(true)
    },
    async checkCompanyDetails(flag) {
      this.layout.showAreaLoading(['.table-list', 'yhpcrwxq'])
      const { status, data } = await checkCompanyDetails({ checkTaskId: this.checkTaskData.checkTaskId, companyId: this.checkTaskData.companyId, moduleName: this.moduleName, pageNum: this.pageNum, pageSize: 10 })
      if (status === 0 && data) {
        this.listData = flag ? this.listData.concat(data.list) : data.list
        this.total = data.total
        this.pages = data.pages
      }
      this.layout.hideAreaLoading(['.table-list', 'yhpcrwxq'])
    },
    async dangersLedgerByCompany() {
      const { status, data } = await dangersLedgerByCompany({ checkTaskId: this.checkTaskData.checkTaskId, companyId: this.checkTaskData.companyId, moduleName: this.moduleName })
      if (status === 0 && data) {
        const obj = {}
        data.forEach((x) => {
          obj[x.typename] = x.coalesce
        })
        this.numForm = obj
        this.$set(this.descData, 'dangersNum', this.numForm['隐患总数'])
      }
    },
    async checkCompanyContentScore() {
      this.layout.showAreaLoading(['.table-list'])
      const { status, data } = await checkCompanyContentScore({ checkTaskId: this.checkTaskData.checkTaskId, companyId: this.checkTaskData.companyId, moduleName: this.moduleName })
      if (status === 0 && data) {
        this.listData = data
      }
      this.layout.hideAreaLoading(['.table-list'])
    }
  }
}
</script>

<style scoped lang="scss">
.history-pop-up{
  background: url("~@/assets/images/wapper.png") no-repeat;
  background-size: 100% auto;
}
</style>
